#pragma once

#include "../3rdSDK/Thread/Thread.h"
#include "BladeMP3Enc.h"

class CMP3_EncoderDlg;
class EncodeThread : public CThread  
{
	// ********** LAME INTERFACE ************
	//Function pointer
	 BE_ERR	beInitStream    (PBE_CONFIG pbeConfig, PDWORD dwSamples, PDWORD dwBufferSize, PHBE_STREAM phbeStream);
	 BE_ERR	beEncodeChunk   (HBE_STREAM hbeStream, DWORD nSamples, PSHORT pSamples, PBYTE pOutput, PDWORD pdwOutput);
	 BE_ERR	beDeinitStream  (HBE_STREAM hbeStream, PBYTE pOutput, PDWORD pdwOutput);
	 BE_ERR	beCloseStream   (HBE_STREAM hbeStream);
	 VOID	beVersion       (PBE_VERSION pbeVersion);
	 VOID	beWriteVBRHeader(LPCSTR lpszFileName);
	 HINSTANCE hLameDLL;
	 HINSTANCE hBladeDLL;

private:
	CString errmsg;
	CString          m_mp3folder;
	CMP3_EncoderDlg* m_pWnd;
	int              m_nFile;

	FILE*       pFileOut;
	MMCKINFO    mmckinfoParent;     // parent chunk information
	MMCKINFO    mmckinfoSubchunk;   // subchunk information structure
	HMMIO	    hmmio;              // file handle for open file 
	HGLOBAL     data;

	BE_CONFIG   beConfig;

	DWORD         dwMP3Buffer;
	HBE_STREAM    hbeStream;
	BE_ERR        err;
	DWORD         dwSamples;
	WAVEFORMATEX* pFormat;           // address of "FMT" chunk
	PBYTE         pMP3Buffer;
	PSHORT        pWAVBuffer;
	
	DWORD offset;	// audio data block offset
	DWORD size;	// size of audio data block
	DWORD dwDone;
	DWORD startTime;
	DWORD chunkLeft;
	DWORD loopTime;
	DWORD currentTime;
	//DWORD timeLeft;
	//DWORD timeElapsed;
	//DWORD timeEstimate;
public:
	DECLARE_DYNAMIC(EncodeThread)

	// Construction & Destruction
	EncodeThread(CMP3_EncoderDlg* pWnd, LPARAM lParam );

	virtual ~EncodeThread();
	// Attributes, Operations
public:
	BOOL bCanContinue;
protected:
	/////////////////////////////////////////////////////////////////////////////////////
	// Main Thread Handler
	// The only method that must be implemented
	virtual	DWORD ThreadHandler();
	/////////////////////////////////////////////////////////////////////////////////////
private:
	LPCTSTR GetInputFile();
	LPCTSTR GetOutputFile();

	void encode();
	BOOL checkInputFile();
	void encodeWithLame();
	void encodeWithBlade(){ ASSERT(false);}

	//helpers
	void Print(LPCTSTR lpszFormat, ...);
	void UpdateProgress(int nPct);
	void SetProgress();
	void SetThreadStatus(LPCTSTR lpszFormat, ...);
};
